Imports System.Runtime.Serialization.Formatters.Binary
Imports OpenArchive.Document.Components.Data

Namespace OpenArchive
	Namespace Formatters.Binary
		''' <summary>
		''' 
		''' </summary>
		''' <remarks></remarks>
		Public Class FileComponentDataBinaryFormatter
			Inherits BinaryComponentFormatter
            Sub New()

            End Sub
            Public Overrides Function IsResponsible(ByVal Type As Type, ByVal Version As Version) As Boolean
                Return MyBase.CheckResponsibility(New FileComponentData(), Type, Version)
            End Function

            Public Overrides Function Deserialize(ByVal serializationStream As Global.System.IO.Stream) As ComponentData
                Dim _filecompdata As FileComponentData = New FileComponentData()
                ' The current position in the underlaying stream have to be after reading Type.Name and Version. These data had to be already read in the responibility check.
                If Reader Is Nothing Then
                    Reader = New IO.ArchiveBinaryReader(serializationStream, New Text.ASCIIEncoding)
                End If
                If Not _filecompdata.GetType().Name = Reader.ReadString() Then
                    Throw New Global.System.Runtime.Serialization.SerializationException("incompatible component data")
                End If
                Reader.ReadVersion()

                _filecompdata.MimeType = Reader.ReadString
                ' ToDo Read Digital Signature, Use FromBinary Function
                _filecompdata.Created = Date.FromBinary(Reader.ReadInt64)
                _filecompdata.Modified = Date.FromBinary(Reader.ReadInt64)
                _filecompdata.Filename = Reader.ReadString
                _filecompdata.Content = Reader.ReadBytes()
                Return _filecompdata
            End Function

            Public Overrides Sub Serialize(ByVal serializationStream As Global.System.IO.Stream, ByVal Datacomponent As ComponentData)
                Dim Filecompdata As FileComponentData = TryCast(Datacomponent, FileComponentData)
                If Filecompdata Is Nothing Then Throw New Global.System.Runtime.Serialization.SerializationException()
                Serialize(serializationStream, Filecompdata)
            End Sub
			Public Overloads Sub Serialize(ByVal serializationStream As Global.System.IO.Stream, ByVal Datacomponent As FileComponentData)
                Try
                    Writer = New IO.ArchiveBinaryWriter(serializationStream, New Text.ASCIIEncoding)
                    MyBase.Serialize(serializationStream, Datacomponent)
                    Writer.Write(Datacomponent.MimeType)
                    Writer.Write(Datacomponent.Created.ToBinary)
                    Writer.Write(Datacomponent.Modified.ToBinary)
                    Writer.Write(Datacomponent.Filename)
                    Writer.Write(DirectCast(Datacomponent.Content, Byte()))
                    Writer = Nothing
                Catch ex As Exception
                    Writer = Nothing
                    Throw New Global.System.Runtime.Serialization.SerializationException(ex.Message)
                End Try
			End Sub
		End Class
	End Namespace
End Namespace

